package com.zhiche.lisa.mdm.controller.outer;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.mdm.dao.model.RangeCalcLog;
import com.zhiche.lisa.mdm.service.IRangeCalcLogService;
import com.zhiche.lisa.mdm.service.impl.RangeCalcServiceImpl;
import com.zhiche.lisa.mdm.service.upload.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * 上传文件计算运距 前端控制器
 * </p>
 *
 * @author qichao
 * @since 2018-08-02
 */
@RestController
@RequestMapping("/rangeCalc")
@Api(value = "/rangeCalc", description = "地址类型接口",tags={"地址类型接口"})
public class RangeCalcController {

    private static final Logger LOGGER = LoggerFactory.getLogger(RangeCalcController.class);

    @Autowired
    private RangeCalcServiceImpl rangeCalcService;

    @Autowired
    private IRangeCalcLogService rangeCalcLogService;

    @Autowired
    private UploadService uploadService;

    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "导入EXCEL并进行计算", notes = "导入EXCEL并进行计算", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Long> importByExcel(@RequestParam(value = "file") MultipartFile file) {
        RestfulResponse<Long> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            Long id = rangeCalcService.importRange(file);
            result.setData(id);
        } catch (BaseException e) {
            LOGGER.error("RangeCalcController.importByExcel error: {}" + e);
            result.setCode(-1);
            result.setMessage(e.getMessage());
        } catch (Exception ex) {
            LOGGER.error("RangeCalcController.importByExcel error: {}"+ ex);
            result.setMessage("导入系统异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/pageRangeCalcLogs")
    @ApiOperation(value = "分页显示日志", notes = "分页显示日志", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<Page<RangeCalcLog>> pageRangeCalcLogs(@RequestBody Page<RangeCalcLog> page) {
        RestfulResponse<Page<RangeCalcLog>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            Page<RangeCalcLog> pageVo = rangeCalcLogService.rangCalcLogLine(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("RangeCalcController.pageRangeCalcLogs error: {}" + e);
            result.setCode(-1);
            result.setMessage(e.getMessage());
        } catch (Exception ex) {
            LOGGER.error("RangeCalcController.pageRangeCalcLogs error: {}"+ ex);
            result.setMessage("");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }


    @GetMapping(value = "/downloadTemplate")
    @ApiOperation(value = "下载模板", notes = "下载模板", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<String> downloadTemplate(){
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
          String key = "RangeTemplate.xlsx";
          String downloadUrl =uploadService.getStrUrl(key);
          result.setData(downloadUrl);
        } catch (BaseException e) {
            LOGGER.error("RangeCalcController.downloadTemplate error: {}" + e);
            result.setCode(-1);
            result.setMessage(e.getMessage());
        } catch (Exception ex) {
            LOGGER.error("RangeCalcController.downloadTemplate error: {}"+ ex);
            result.setMessage("");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @GetMapping(value = "/downloadFile")
    @ApiOperation(value = "下载文件", notes = "下载文件", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    public RestfulResponse<String> downloadFileByKey(String fileName){
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            String downloadUrl =rangeCalcLogService.getUrlByFileName(fileName);
            result.setData(downloadUrl);
        } catch (BaseException e) {
            LOGGER.error("RangeCalcController.downloadFileByKey error: {}" + e);
            result.setCode(-1);
            result.setMessage(e.getMessage());
        } catch (Exception ex) {
            LOGGER.error("RangeCalcController.downloadFileByKey error: {}"+ ex);
            result.setMessage("");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

}

